package com.IC.base.controller;

import IC.common.core.map.VO.AllCountVO;
import IC.common.core.map.VO.HouseTypeVO;
import IC.common.core.map.VO.PopulationCount;
import com.IC.base.service.PlotService;
import IC.common.core.domain.R;
import com.IC.domain.dto.PlotDTO;
import com.IC.domain.po.ActualTenementPO;
import com.github.pagehelper.PageInfo;
import com.IC.domain.po.PlotPO;
import com.IC.domain.vo.PlotVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/plot")
public class PlotController {

    @Autowired
    private PlotService plotService;
    @Autowired
    private RedisTemplate<String, Object> baseRedisTemplate;

    @PostMapping("/page")
    public R page(@RequestBody PlotDTO plotDTO) {
        PageInfo<PlotVO> findlist = plotService.findlist(plotDTO);
        return R.ok(findlist);
    }

    @GetMapping("/list")
    public R list(@RequestParam("pageNum")Integer pageNum,@RequestParam("pageSize")Integer pageSize){
        PageInfo<PlotVO> all = plotService.findAll(pageNum,pageSize);
        return R.ok(all);
    }

    @GetMapping("/findByPlotId/{id}")
    public R findByPlotId(@PathVariable Long id){
        PlotVO all = plotService.findByPlotId(id);
        return R.ok(all);
    }

    @GetMapping("/findAll")
    public R findAll(){
        List<PlotPO> all = all = plotService.findPlotAll();
        return R.ok(all);
    }

    @PostMapping("/add")
    public R add(@RequestBody PlotPO plotPO) {
        plotService.add(plotPO);
        refreshCache();
        return R.ok();
    }

    @GetMapping("/delete/{id}")
    public R delete(@PathVariable("id")Long id) {
        plotService.removeById(id);
        refreshCache();
        return R.ok();
    }

    @PostMapping("/update")
    public R update(@RequestBody PlotPO plotPO) {
        plotService.updateById(plotPO);
        refreshCache();
        return R.ok();
    }


    @GetMapping("/findPlot/{id}")
    public R page(@PathVariable("id") Integer id) {
        List<PlotVO> findlist = plotService.findplot(id);
        return R.ok(findlist);
    }

    @GetMapping("/findOutline/{id}")
    public R findOutline(@PathVariable("id") Integer id) {
        String outline = plotService.findOutline(id);
        return R.ok(outline);
    }



    @GetMapping("/findAllCount")
    public R findAllCount() {
        List<AllCountVO> allcount=plotService.findAllCount();
        return R.ok(allcount);
    }

    @GetMapping("/findPopulationCount")
    public R findpopulationcount() {
        PopulationCount populationcount=plotService.findPopulationCount();
        return R.ok(populationcount);
    }

    @GetMapping("findHouseTypeCount")
    public R findHouseTypeCount() {
        HouseTypeVO housecount=plotService.findHouseTypeCount();
        return R.ok(housecount);
    }

    @GetMapping("/findPlotLists")
    public R list(){
        List<PlotVO> all = plotService.findPlotLists();
        return R.ok(all);
    }

    /**
     * 根据行政区划查找小区
     */
    @GetMapping("/findByDivisionId/{id}")
    public R findByDivisionId(@PathVariable Long id){
        List<PlotPO> byDivisionId = plotService.findByDivisionId(id);
        return R.ok(byDivisionId);
    }

    @GetMapping("/findAllOutLine")
    public R findAllOutLine() {
        List<String> all = plotService.findAllOutLine();
        return R.ok(all);
    }

    private void cacheAllData() {
        List<PlotPO> list = plotService.select();
        baseRedisTemplate.opsForValue().set("Plot", list);
    }

    @Scheduled(fixedRate = 10000) // 每10秒执行一次
    public void refreshCache() {
        cacheAllData();
    }

}
